package br.com.felinelayer.common;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Message
 */
public final class Message {

    private JSONObject jsonObj;

    public Message() {
        setFromString( "" );
    }

    public Message( String name ) {
        setFromString( "" ).setName( name );
    }

    public Message setFromString( String fullString ) {
        try {
            if ( fullString.isEmpty() )
                jsonObj = new JSONObject();
            else
                jsonObj = new JSONObject( fullString );
        } catch ( JSONException ex ) {
            Logger.getLogger( Message.class.getName() ).log( Level.SEVERE, null, ex );
        }
        return this;
    }

    public String getName() {
        return getValueByName( Protocol.MSG_NAME );
    }

    public Message setName( String name ) {
        setValueByName( Protocol.MSG_NAME, name );
        return this;
    }

    @Override
    public String toString() {
        return jsonObj.toString();
    }

    public long getLongByName( String name ) {
        return Long.parseLong( getValueByName( name ) );
    }

    public int getIntByName( String name ) {
        return Integer.parseInt( getValueByName( name ) );
    }

    public String getValueByName( String name ) {
        try {
            return jsonObj.getString( name );
        } catch ( JSONException ex ) {
            Logger.getLogger( Message.class.getName() ).log( Level.SEVERE, null, ex );
        }
        return "";
    }

    public Message setValueByName( String name, long value ) {
        return setValueByName( name, value + "" );
    }

    public Message setValueByName( String name, int value ) {
        return setValueByName( name, value + "" );
    }

    public Message setObjectByName( String name, Object value ) {
        try {
            jsonObj.put( name, value );
        } catch ( JSONException ex ) {
            Logger.getLogger( Message.class.getName() ).log( Level.SEVERE, null, ex );
        }
        return this;
    }

    public Message setValueByName( String name, String value ) {
        return setObjectByName( name, value );
    }

    public boolean isPropertyExists( String name ) {
        return !jsonObj.isNull( name );
    }
    private static String ARRAY = "array";

    public boolean isArray() {
        return isPropertyExists( ARRAY );
    }

    public Message deleteValue( String name ) {
        jsonObj.remove( name );
        return this;
    }

    public ArrayList<Message> getArray() {
        ArrayList<Message> result = new ArrayList<>();
        JSONObject j;
        Message c;
        try {
            JSONArray ja = jsonObj.getJSONArray( ARRAY );
            for ( int i = 0; i < ja.length(); i++ ) {
                j = ja.getJSONObject( i );
                c = new Message().setFromString( j.toString() );
                result.add( c );
            }
        } catch ( JSONException ex ) {
            Logger.getLogger( Message.class.getName() ).log( Level.SEVERE, null, ex );
        }
        return result;
    }

    public static Message arrayToMessage( ArrayList<Message> messages ) {
        JSONArray ja = new JSONArray();
        try {
            for ( Message c : messages )
                ja.put( new JSONObject( c.toString() ) );
        } catch ( JSONException ex ) {
            Logger.getLogger( Message.class.getName() ).log( Level.SEVERE, null, ex );
        }
        return new Message( ARRAY ).setObjectByName( ARRAY, ja );
    }

    public String[] getNames() {
        return JSONObject.getNames( jsonObj );
    }

    public Message insertValuesFrom( Message message ) {
        for ( String n : message.getNames() ) {
            if ( n.equals( Protocol.MSG_NAME ) )
                continue;
            setValueByName( n, message.getValueByName( n ) );
        }
        return this;
    }
//    public static void main( String[] args ) {
//        // 
//        Message c1 = new Message( "create" );
//        c1.setValueByName( "id", "1" );
//        Message c2 = new Message( "create" );
//        c2.setValueByName( "id", "2" );
//        Message c3 = new Message( "create" );
//        c3.setValueByName( "id", "3" );
//        // 
//        ArrayList<Message> list = new ArrayList<>();
//        list.add( c1 );
//        list.add( c2 );
//        list.add( c3 );
//        for ( Message rc : list )
//            System.out.println( rc.toString() );
//        // 
//        Message r = Message.arrayToMessage( list );
//        System.out.println( r.toString() );
//        if ( r.isArray() )
//            System.out.println( "is array ok" );
//        ArrayList<Message> rl = r.getArray();
//        for ( Message rc : rl )
//            System.out.println( rc.toString() );
//    }
}
